<template>
  <div class="app-container">
    <el-form
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="提交时间">
        <el-date-picker
          v-model="datePickerValue"
          :picker-options="pickerOptions"
          type="datetimerange"
          :editable="false"
          :clearable="false"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
          @change="selectDate"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select
          v-model="queryParams.auditStatus"
          placeholder="请选择审核状态"
          clearable
          size="small"
          style="line-height: 30px"
        >
          <el-option
            v-for="item in channelStatus"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      border
      :data="auditList"
      :height="clientHeight"
      :max-height="clientHeight"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center"  prop="id" /> -->
      <el-table-column
        label="市场建档id"
        align="center"
        prop="marketReservationId"
      />
      <!-- <el-table-column label="资源地址" align="center" prop="url" /> -->
      <el-table-column label="新建档人" align="center" prop="newFilingPerson" />
      <el-table-column label="新跟进人" align="center" prop="newFollowPerson" />
      <el-table-column
        label="新归属人"
        align="center"
        prop="newAffiliationPerson"
      />
      <el-table-column label="新一级渠道" align="center" prop="channelFirst" />
      <el-table-column label="新二级渠道" align="center" prop="channelSecond" />
      <el-table-column label="新三级渠道" align="center" prop="channelThird" />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          {{
            scope.row.auditStatus == 0
              ? '待审核'
              : scope.row.auditStatus == 1
              ? '审核已通过'
              : '审核未通过'
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="提交时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditPerson" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template v-if="scope.row.auditStatus == 0" slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row, '通过')"
            >通过</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row, '不通过')"
            >不通过</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listAudit,
  getAudit,
  delAudit,
  addAudit,
  updateAudit,
  exportAudit,
} from '@/api/erp/audit'
import { getTodayStartWithEnd } from '@/common/getDate'
import { dateMap } from '@/common/date'
import { mapState, mapGetters } from 'vuex'

export default {
  name: 'Audit',
  data() {
    return {
      datePickerValue: [],
      pickerOptions: {
        shortcuts: [
          {
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', dateMap['今日'])
            },
          },
          {
            text: '昨天',
            onClick(picker) {
              picker.$emit('pick', dateMap['昨日'])
            },
          },
          {
            text: '本周',
            onClick(picker) {
              picker.$emit('pick', dateMap['本周'])
            },
          },
          {
            text: '本月',
            onClick(picker) {
              picker.$emit('pick', dateMap['本月'])
            },
          },
          {
            text: '同比',
            onClick(picker) {
              picker.$emit('pick', dateMap['同比'])
            },
          },
        ],
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 渠道修改审核表格数据
      auditList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        auditStatus: undefined,
        auditTime: undefined,
        orderByColumn: 'createTime',
        isAsc: 'desc',
      },
      // 表单参数
      form: {},
      // 表单校验
      datePickerKey: '今日',
      clientHeight: document.body.clientHeight - 223,
    }
  },
  computed: {
    ...mapGetters('dict', ['channelStatus']),
    ...mapState({
      userName: (state) => state.user.name,
    }),
  },
  watch: {
    datePickerKey: {
      handler() {
        this.handleDateButtonKeyChange()
      },
      immediate: true,
    },
  },
  methods: {
    selectDate(e) {
      if (e == null) {
        this.getList()
      } else {
        if (e[0].$d !== undefined) {
          this.queryParams.beginTime = e[0].$d
          this.queryParams.endTime = e[1].$d
        } else {
          this.queryParams.beginTime = e[0]
          this.queryParams.endTime = e[1]
        }
        this.getList()
      }
    },
    handleDateButtonKeyChange() {
      this.datePickerValue = getTodayStartWithEnd()
      this.changeTime(this.datePickerValue)
    },
    changeTime(e) {
      // this.queryParams.beginTime = this.parseTime(e[0].$d, '{y}-{m}-{d} {h}:{i}:{s}')
      //   this.queryParams.endTime = this.parseTime(e[1].$d, '{y}-{m}-{d} {h}:{i}:{s}')
      this.queryParams.beginTime = e[0].$d
      this.queryParams.endTime = e[1].$d
      this.getList()
    },
    /** 查询渠道修改审核列表 */
    getList() {
      this.loading = true
      listAudit(this.queryParams).then((response) => {
        this.auditList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        marketReservationId: undefined,
        clinicPersonId: undefined,
        url: undefined,
        newFilingPerson: undefined,
        newFollowPerson: undefined,
        newAffiliationPerson: undefined,
        channelFirst: undefined,
        channelSecond: undefined,
        channelThird: undefined,
        mediumFirst: undefined,
        mediumSecond: undefined,
        mediumThird: undefined,
        auditStatus: '0',
        auditTime: undefined,
        auditPerson: undefined,
        remarks: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    async handleUpdate(row, msg) {
      const queryParams = {
        id: row.id,
      }
      await this.$confirm(`是否确认审核${msg}该条数据项?`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
      if (msg == '通过') {
        queryParams.auditStatus = 1
        queryParams.auditPerson = this.userName
      } else {
        queryParams.auditStatus = 2
        queryParams.auditperson = this.userName
      }
      console.log(queryParams)
      await updateAudit(queryParams)
      this.msgSuccess('审核成功')
      await this.getList()
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有渠道修改审核数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return exportAudit(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
        .catch(function () {})
    },
  },
}
</script>
